<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * FormativeLab
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		FormativeLab
 * @author		FormativeLab Dev Team
 * @copyright	Copyright (c) 2011 - 2013, FormativeLab, Inc.
 * @license		http://www.formativesoft.com/license/
 * @link		http://www.formativesoft.com/
 * @since		Version 2.0
 * @filesource ./application/libraries/Renderer.php
 */

// ------------------------------------------------------------------------

/**
 * FormativeLab Renderer Library
 *
 * @package		FormativeLab
 * @subpackage	libraries
 * @category	libraries
 * @author		Formative Soft Ltd
 * @link		http://www.formativesoft.com/
 */

// ------------------------------------------------------------------------

/**
 * AbstractRenderer is a abstract class. This class is the parent class of
 * Renderer class. Thats why it's very much import to laod AbstractRenderer
 * php file.
 **/
require_once(APPPATH."libraries/AbstractRenderer.php");

/**
 * Renderer class use for render view in CodeIgniter MVC. This class used
 * to reduce boiler plate code of controllers. Renderer class use to auto
 * load view according to the controller and action name. This class was 
 * developed by the inspiration of groovy on grails. To Autoload views and
 * respectives layout.
 **/
class Renderer extends AbstractRenderer {

	/**
	 * Constraints declaration for different type of layouts used by the web 
	 * application. these layout constraints are useful render different view
	 * different way.
	 **/
	const CATEGORY			= 'categories';
	const DEFAULTS			= 'defaults';
	const EXTMVC			= 'extmvc';
	const JAVASCRIPT		= 'javascript';
	const OFFLINEDB			= 'offlinedb';
	const SECTIONS			= 'sections';
	const STYLESHEET		= 'stylesheet';
	const BLOG				= 'blog';
		
	/**
	 * __construct() used to initialized intance objects, properties and 
	 * configuration. It's also load required library and helpers
	 *
	 * @access	public
	 * @return  void 
	 **/
    public function __construct($layout	= self::DEFAULTS){
		parent::__construct($layout);
    }
	
	/**
	 * response() with two parameter use to load view dynamically. Both parameters
	 * are optinal. this method is use to response view dynamically as it layout
	 * and others fragments.
	 *
	 * @access	public
	 * @param	data
	 * @param 	viewPath
	 * @return  void 
	 **/
	public function response(Array $data = array(), $viewPath = NULL){
		$this->resolveModuleControllerAction();
		$layout	= $this->resolveLayoutName();
	
		switch($layout){
			case self::CATEGORY		: break;
			case self::DEFAULTS 	: $this->loadDefaultsLayout($data, $viewPath);		break;			
			case self::EXTMVC		: $this->loadExtmvcLayout($data, $viewPath);		break;
			case self::JAVASCRIPT	: $this->loadJavaScriptLayout($data, $viewPath);	break;
			case self::OFFLINEDB	: $this->loadOfflineDBLayout($data, $viewPath);		break;
			case self::SECTIONS		: break;
			case self::STYLESHEET	: $this->loadStyleSheetLayout($data, $viewPath);	break;
			case self::BLOG			: break;
			default					: $this->loadDefaultsLayout($data, $viewPath);	break;
		}		
	}
		
	/**
	 * loadDefaultsLayout() with two optional parameters. this method use to load
	 * defaults layout with differnt types of banners, elements, footers, menus and
	 * sidebars.
	 *
	 * @access	public
	 * @param	data
	 * @param 	viewPath
	 * @return  void 
	 **/
	private function loadDefaultsLayout(Array $data = array(), $viewPath = NULL){
		$layout 	= array(
			'banner'	=> $this->loadBanner('topbanner'),
			'element'	=> $this->loadElement('carosel'),
			'footer'	=> $this->loadFooter('footer'),
			'menus'		=> $this->loadMenu('topmenu'),
			'sidebar'	=> $this->loadSidebar('rightsidebar'),
			'content'	=> $this->loadView($viewPath, $data)
		);
		$this->loadLayout($layout);
	}
	
	/**
	 * loadExtmvcLayout() with two optional parameters. this method use to load
	 * defaults layout with differnt types of title Layout, header Layout, body
	 * Layout
	 *
	 * @access	public
	 * @param	data
	 * @param 	viewPath
	 * @return  void 
	 **/
	private function loadExtmvcLayout(Array $data = array(), $viewPath = NULL){
		$layout 	= array(
			'layoutTitle'	=> 'Formative Soft Extmvc',
			'layoutHeader'	=> NULL,
			'layoutBody'	=> $this->loadView($viewPath, $data)
		);
		$this->loadLayout($layout);
	}
	
	/**
	 * loadJavaScriptLayout() with two optional parameters. this method use to load
	 * defaults layout with differnt types of title Layout, header Layout, body
	 * Layout
	 *
	 * @access	public
	 * @param	data
	 * @param 	viewPath
	 * @return  void 
	 **/
	private function loadJavaScriptLayout(Array $data = array(), $viewPath = NULL){
		$this->controller->output->set_status_header(200);
		$this->controller->output->set_header('Content-type: application/javascript; charset=utf-8');
		$layout 	= array(
			'layoutBody'	=> $this->loadView($viewPath, $data)
		);
		$this->loadLayout($layout);
	}
	
	/**
	 * loadStyleSheelLayout() with two optional parameters. this method use to load
	 * defaults layout with differnt types of title Layout, header Layout, body
	 * Layout
	 *
	 * @access	public
	 * @param	data
	 * @param 	viewPath
	 * @return  void 
	 **/
	private function loadStyleSheetLayout(Array $data = array(), $viewPath = NULL){
		$this->controller->output->set_status_header(200);
		$this->controller->output->set_header('Content-type: text/css; charset=utf-8');
		$layout 	= array(
			'layoutBody'	=> $this->loadView($viewPath, $data)
		);
		$this->loadLayout($layout);
	}
	
	/**
	 * loadOfflineDBLayout() with two optional parameters. this method use to load
	 * defaults layout with differnt types of title Layout, header Layout, body
	 * Layout
	 *
	 * @access	public
	 * @param	data
	 * @param 	viewPath
	 * @return  void 
	 **/
	private function loadOfflineDBLayout(Array $data = array(), $viewPath = NULL){
		$this->controller->output->set_status_header(200);
		$this->controller->output->set_header('Content-type: application/javascript; charset=utf-8');
		$offlinedb	= array(
			'offlinedb' => filterRecords($data)
		);
		$layout 	= array(
			'layoutBody'	=> $this->loadView($viewPath, $offlinedb)
		);
		$this->loadLayout($layout);
	}

}

/* End of file Renderer.php */
/* Location: ./application/libraries/Renderer.php */
